package com.yangming.springboot.controller;

import com.yangming.springboot.base.ResultInfo;
import com.yangming.springboot.exceptions.ParamsException;
import com.yangming.springboot.model.UserModel;
import com.yangming.springboot.query.UserQuery;
import com.yangming.springboot.service.UserService;
import com.yangming.springboot.utils.AssertUtil;
import com.yangming.springboot.utils.LoginUserUtil;
import com.yangming.springboot.vo.User;
import io.swagger.models.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @GetMapping(value = "hello")
    public String helloSpring(){
        return "hello";
    }

    @GetMapping(value = "user/name/{userName}")
    public User queryUserByName(@PathVariable("userName") String userName){
        return userService.queryUserByName(userName);
    }

    @GetMapping(value = "user/id/{id}")
    public User queryUserById(@PathVariable("id") Integer id){
        return userService.queryUserById(id);
    }

    @PutMapping(value = "user", consumes = "application/json")
    public ResultInfo saveUser(@RequestBody User user){
        userService.saveUser(user);
        return success("save success");
    }

    @PostMapping(value = "user")
    public ResultInfo updateUser(@RequestBody User user){

        userService.update(user);
        return success("update success");
    }

    @DeleteMapping(value = "user/del/{id}")
    @ResponseBody
    public ResultInfo delete(@PathVariable("id") Integer[] id){

        userService.deleteUser(id);
        return success("delete success");
    }


    @PostMapping(value = "user/login")
    @ResponseBody //表示返回值是一个json格式的内容 ,对应的POSTMAN中使用的是POST-BODY-x-www-form-urlencoded
    public ResultInfo login(String userName, String userPwd){
        System.out.println(userName);
        System.out.println(userPwd);
        ResultInfo resultInfo = new ResultInfo();
        try{
            UserModel userModel = userService.login(userName, userPwd);
            resultInfo.setResult(userModel);
        }catch(ParamsException e){
            e.printStackTrace();
            resultInfo.setCode(e.getCode());
            resultInfo.setMsg(e.getMsg());
        }catch(Exception e){
            e.printStackTrace();
            resultInfo.setCode(500);
            resultInfo.setMsg("operate, error");
        }
        return resultInfo;
    }

    @PostMapping(value = "user/updatePassword")
    @ResponseBody
    public ResultInfo updatePassword(HttpServletRequest request, String oldPassword, String newPassword, String comfirmPassword){
        System.out.println(request);
        System.out.println(oldPassword);
        System.out.println(newPassword);
        System.out.println(comfirmPassword);
        ResultInfo resultInfo = new ResultInfo();
        try{
            userService.updateUserPassword(LoginUserUtil.releaseUserIdFromCookie(request),oldPassword, newPassword, comfirmPassword);
        }catch(ParamsException e){
            e.printStackTrace();
            resultInfo.setCode(e.getCode());
            resultInfo.setMsg(e.getMsg());
        } catch (Exception e){
            e.printStackTrace();
            resultInfo.setCode(500);
            resultInfo.setMsg("error");
        }

        return resultInfo;
    }

    @GetMapping(value = "user/queryAllSales")
    @ResponseBody
    public List<Map<String, Object>> queryAllSales(){
        return userService.queryAllSale();
    }

    @RequestMapping(value = "user/list")
    @ResponseBody
    public Map<String, Object> queryUserByParams(UserQuery userQuery){
        return userService.queryUserByParams(userQuery);
    }
 @RequestMapping(value = "user/addOrUpdateUserPage")
    public String addOrUpdateUserPage(Integer id, Model model){
//        model.addArreibute("user", userService.queryUserById(id));//这个是往前端作用域里面加数据用的，不过暂时不知道怎么用
        return "user/add_update"; //这个是前后端不分离的情况下做视图转发功能的
    }

    @RequestMapping(value = "user/userindex")
    public String index(){
        return "user/user";
    }

}
